package com.LeetCode.Math;

import org.junit.Test;

/**
 * 字符串乘法
 */
public class multiply {

    public String multiply(String num1, String num2) {
        int n1 = num1.length();
        int n2 = num2.length();
        if(n1<0 || n2<0) return "";
        int[] mul = new int[n1 + n2];
        for(int i = n1 - 1; i >=0; i--){
            for(int j = n2 - 1; j >= 0; j--){
                int bitmul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
                 mul[i+j+1] += bitmul;
            }
        }
        for(int i = n1 + n2 - 1; i>0; i--){
            mul[i - 1] += mul[i] / 10;//把进位加过去
            mul[i] %= 10;
        }
        int index = mul[0] == 0 ? 1 : 0;
        StringBuffer ans = new StringBuffer();
        while (index < n1 + n2) {
            ans.append(mul[index]);
            index++;
        }
        return ans.toString();

    }


    @Test
    public void test(){
        System.out.println(multiply("1234","456"));
    }
}
